package com.haixiaoke.saas.apartment.service.impl;

import java.util.List;

import cn.hutool.json.JSONUtil;
import com.haixiaoke.saas.apartment.domain.UnsubscribeRule;
import com.haixiaoke.saas.apartment.mapper.UnsubscribeRuleMapper;
import com.haixiaoke.saas.apartment.service.IUnsubscribeRuleService;
import com.haixiaoke.saas.common.constant.HouseConstants;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.common.tenant.TenantContext;
import com.haixiaoke.saas.common.utils.DateUtils;
import com.haixiaoke.saas.common.utils.GsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 退订规则Service业务层处理
 *
 * @author panda
 * @date 2024-03-30
 */
@Service
public class UnsubscribeRuleServiceImpl implements IUnsubscribeRuleService {
    @Autowired
    private UnsubscribeRuleMapper unsubscribeRuleMapper;

    /**
     * 查询退订规则
     *
     * @param unsubscribeRuleId 退订规则主键
     * @return 退订规则
     */
    @Override
    public UnsubscribeRule selectUnsubscribeRuleByUnsubscribeRuleId(Long unsubscribeRuleId) {
        UnsubscribeRule unsubscribeRule = unsubscribeRuleMapper.selectUnsubscribeRuleByUnsubscribeRuleId(unsubscribeRuleId);
        if (HouseConstants.UNSUBSCRIBE_TYPE_HOTEL.equals(unsubscribeRule.getUnsubscribeType())) {
            unsubscribeRule.setUnsubscribeContentMap(GsonUtil.toMap(unsubscribeRule.getUnsubscribeContent()));
        }
        return unsubscribeRule;
    }

    /**
     * 查询退订规则
     *
     * @param unsubscribeRuleId 退订规则主键
     * @return 退订规则
     */
    @Override
    public UnsubscribeRule selectUnsubscribeRuleById(Long unsubscribeRuleId) {
        return unsubscribeRuleMapper.selectUnsubscribeRuleByUnsubscribeRuleId(unsubscribeRuleId);
    }

    /**
     * 查询退订规则列表
     *
     * @param unsubscribeRule 退订规则
     * @return 退订规则
     */
    @Override
    public List<UnsubscribeRule> selectUnsubscribeRuleList(UnsubscribeRule unsubscribeRule) {
        return unsubscribeRuleMapper.selectUnsubscribeRuleList(unsubscribeRule);
    }

    /**
     * 新增退订规则
     *
     * @param unsubscribeRule 退订规则
     * @return 结果
     */
    @Override
    public int insertUnsubscribeRule(UnsubscribeRule unsubscribeRule) {
        unsubscribeRule.setCreateTime(DateUtils.getNowDate());
        if (HouseConstants.UNSUBSCRIBE_TYPE_HOTEL.equals(unsubscribeRule.getUnsubscribeType())) {
            unsubscribeRule.setUnsubscribeContent(JSONUtil.toJsonStr(unsubscribeRule.getUnsubscribeContentMap()));
        } else {
            unsubscribeRule.setUnsubscribeContent("");
        }
        return unsubscribeRuleMapper.insertUnsubscribeRule(unsubscribeRule);
    }

    /**
     * 修改退订规则
     *
     * @param unsubscribeRule 退订规则
     * @return 结果
     */
    @Override
    public int updateUnsubscribeRule(UnsubscribeRule unsubscribeRule) {
        unsubscribeRule.setUpdateTime(DateUtils.getNowDate());
        if (HouseConstants.UNSUBSCRIBE_TYPE_HOTEL.equals(unsubscribeRule.getUnsubscribeType())) {
            unsubscribeRule.setUnsubscribeContent(JSONUtil.toJsonStr(unsubscribeRule.getUnsubscribeContentMap()));
        } else {
            unsubscribeRule.setUnsubscribeContent("");
        }
        return unsubscribeRuleMapper.updateUnsubscribeRule(unsubscribeRule);
    }

    /**
     * 批量删除退订规则
     *
     * @param unsubscribeRuleIds 需要删除的退订规则主键
     * @return 结果
     */
    @Override
    public int deleteUnsubscribeRuleByUnsubscribeRuleIds(Long[] unsubscribeRuleIds) {
        return unsubscribeRuleMapper.deleteUnsubscribeRuleByUnsubscribeRuleIds(unsubscribeRuleIds);
    }

    /**
     * 删除退订规则信息
     *
     * @param unsubscribeRuleId 退订规则主键
     * @return 结果
     */
    @Override
    public int deleteUnsubscribeRuleByUnsubscribeRuleId(Long unsubscribeRuleId) {
        return unsubscribeRuleMapper.deleteUnsubscribeRuleByUnsubscribeRuleId(unsubscribeRuleId);
    }

    @Override
    public boolean checkRuleName(UnsubscribeRule unsubscribeRule) {
        int result = unsubscribeRuleMapper.checkRuleName(unsubscribeRule);
        return result > 0;
    }

    @Override
    public int updateUnsubscribeRuleState(UnsubscribeRule unsubscribeRule) {
        UnsubscribeRule rule = unsubscribeRuleMapper.selectUnsubscribeRuleByUnsubscribeRuleId(unsubscribeRule.getUnsubscribeRuleId());
        if (!unsubscribeRule.getTenantId().equals(rule.getTenantId())) {
            throw new ServiceException("数据异常");
        }
        if (HouseConstants.UNSUBSCRIBE_STATE_SHOW.equals(rule.getUnsubscribeState())) {
            if (this.checkAllocationByRule(unsubscribeRule.getUnsubscribeRuleId())) {
                throw new ServiceException("退订规则已关联房间，变更失败");
            }
            unsubscribeRule.setUnsubscribeState(HouseConstants.UNSUBSCRIBE_STATE_CONCEAL);
        } else if (HouseConstants.UNSUBSCRIBE_STATE_CONCEAL.equals(rule.getUnsubscribeState())) {
            unsubscribeRule.setUnsubscribeState(HouseConstants.UNSUBSCRIBE_STATE_SHOW);
        }
        return unsubscribeRuleMapper.updateUnsubscribeRule(unsubscribeRule);
    }

    @Override
    public boolean checkAllocationByRule(Long unsubscribeRuleId) {
        int result = unsubscribeRuleMapper.checkAllocationByRule(unsubscribeRuleId);
        return result > 0;
    }
}
